package cn.edu.jxau.test;

import cn.edu.jxau.util.LinkedQueue;
import cn.edu.jxau.util.Queue;

/**
 * @author Fudashi
 */
public class Main {

	public static void main(String[] args) {

		Stack<Integer> queue = new Stack<>();
		queue.push(1);
		queue.push(2);
		queue.push(3);
		System.out.println(queue.pop());
		queue.push(4);
		queue.push(5);
		queue.push(6);
		System.out.println(queue.pop());
		queue.push(7);
		System.out.println(queue.pop());
		queue.push(8);
		System.out.println(queue.pop());
		System.out.println(queue.pop());
		queue.push(9);
		System.out.println(queue.pop());
		System.out.println(queue.pop());
		System.out.println(queue.pop());

	}
}

/**
 * 用两个Queue实现一个堆栈
 * 
 * @author Fudashi
 */
class Stack<T> {

	private Queue<T> queue1;
	private Queue<T> queue2;

	public Stack() {
		queue1 = new LinkedQueue<>();
		queue2 = new LinkedQueue<>();
	}

	public void push(T data) {

		if (queue1.isEmpty()) {
			queue2.enqueue(data);
		} else {
			queue1.enqueue(data);
		}
	}

	public T pop() {

		if (queue1.isEmpty() && queue2.isEmpty()) {
			throw new RuntimeException("the queue is empty");
		}
		if (queue1.isEmpty()) { //
			int size = queue2.size();
			while (size > 1) {
				queue1.enqueue(queue2.dequeue());
				size--;
			}
			return queue2.dequeue();
		} else {
			int size = queue1.size();
			while (size > 1) {
				queue2.enqueue(queue1.dequeue());
				size--;
			}
			return queue1.dequeue();
		}
	}
}